package com.example.yls.demoa;

import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnSendWeather;
    private Button btnSendTrafic;
    private MyBroadcastRecevier recevier;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recevier = new MyBroadcastRecevier();
        IntentFilter filter = new IntentFilter();
        filter.addAction("Weather_Report");
        filter.addAction("Trafic_Report");
        registerReceiver(recevier, filter);

        initSendWeather();
        initSendTrafic();
        init();


    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        unregisterReceiver(recevier);
    }


    /**
     * 发送天气广播
     */
    private void initSendWeather(){
        btnSendWeather = findViewById(R.id.btn_sendWeather);
        btnSendWeather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("Weather_Report");
                intent.putExtra("Weather","sunny");
                intent.putExtra("temprature",25);
                sendBroadcast(intent);

            }
        });
    }

    /**
     * 发送交通广播
     */
    private void initSendTrafic(){
        btnSendTrafic = findViewById(R.id.btn_sendTrafic);
        btnSendTrafic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("Trafic_Report");
                intent.putExtra("Trafic","堵车");
                intent.putExtra("Time",40);
                sendBroadcast(intent);
            }
        });
    }

    /**
     *
     */
    private void init(){

    }
}

